package org.zxn.study.utils;

import java.util.Random;

/**
 * @ClassName StringUtil
 * @Author zxn
 * @Date 2025/7/10 17:22
 **/
public class StringUtil {
    // 默认随机字符串可选字符集
    private static final String CHAR_LOWER = "abcdefghijklmnopqrstuvwxyz";
    private static final String CHAR_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String DIGIT = "0123456789";
    private static final String ALPHA_NUMERIC = CHAR_LOWER + CHAR_UPPER + DIGIT;

    // 生成指定长度的随机字符串（由字母数字组成）
    public static String generateRandomString(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("Length must be a positive integer.");
        }

        Random random = new Random();
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(ALPHA_NUMERIC.length());
            sb.append(ALPHA_NUMERIC.charAt(index));
        }

        return sb.toString();
    }
}
